import _ from 'lodash'
export const indexMixin = {
	methods: {
		handleAdd (operateRef, defaultValue) {
			this.$refs[operateRef].init(null, defaultValue)
		},
		handleEdit (operateRef, row) {
			this.$refs[operateRef].init(row)
		}
	}
}

export const modalFormMixin = {
	data () {
		return {
			dialogVisible: false,
			isEdit: false,
			title: '新增'
		}
	},
	methods: {
		init (row, defaultValue = {}) {
			if (row) {
				this.title = '编辑'
				this.formItem = _.clone(row)
				this.isEdit = true
				delete this.formItem.updateTime
			} else {
				this.title = '新增'
				this.formItem = _.clone(defaultValue)
				this.isEdit = false
			}
			this.dialogVisible = true
		}
	}
}

export const resourceMixin = {
	computed: {
		resources () {
			const userInfo = localStorage.getItem('userInfo')
			if (userInfo) {
				const arr = JSON.parse(userInfo).resource
				if (Array.isArray(arr)) {
					return arr
				} else {
					return []
				}
			} else {
				return []
			}
		}
	}
}
